﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Windows.Controls;
using System.Windows.Media.Animation;



namespace offscreen_buffer_test_1
{
   class Program
   {
      static Window w;
      static Grid g;

      static DoubleAnimation da;
      static TranslateTransform tt;

      [STAThread]
      static void Main(string[] args)
      {
          w = new Window();
          w.Width = 1400;
         System.Windows.Application app = new System.Windows.Application();

         RenderOptions.SetBitmapScalingMode(w, BitmapScalingMode.NearestNeighbor);

           g = new Grid();


          g.ColumnDefinitions.Add(new ColumnDefinition());

          g.ColumnDefinitions.Add(new ColumnDefinition());

          g.ColumnDefinitions.Add(new ColumnDefinition());

          g.ColumnDefinitions[1].Width = new GridLength(1);

          g.Background = System.Windows.Media.Brushes.Black;


          for (int n = 5; n < 20; ++n) {
             g.RowDefinitions.Add( new RowDefinition() );

             text_canvas tc = new text_canvas(n, "GDI+ Segoe UI 을 시작페이지로 하세요!|채용정보|광고 프로그램| 간단한 키보드 조작으로 응용 프로그램 및 파일을 찾아 실행\n[File] [Edit] [View] [Refactor] [Project] [Build] [Debug]", 700, (int)(n * 3.7), "Segoe UI");
             Grid.SetColumn(tc, 0);
             Grid.SetRow(tc, 2 * (n - 5));
             RenderOptions.SetBitmapScalingMode(tc, BitmapScalingMode.NearestNeighbor);
             g.Children.Add(tc);




             TextBlock tb = new TextBlock();
             tb.Text = "WPF Segoe UI 을 시작페이지로 하세요!|채용정보|광고 프로그램| 간단한 키보드 조작으로 응용 프로그램 및 파일을 찾아 실행\n[File] [Edit] [View] [Refactor] [Project] [Build] [Debug]";
             tb.FontFamily = new System.Windows.Media.FontFamily("Segoe UI");
             tb.FontSize = n * 1.333333333333333333;
             tb.SnapsToDevicePixels = true;
             tb.Background = System.Windows.Media.Brushes.White;

             Grid.SetColumn(tb, 2);
             Grid.SetRow(tb, 2 * (n - 5));
             g.Children.Add(tb);

             g.RowDefinitions.Add(new RowDefinition());
             g.RowDefinitions[g.RowDefinitions.Count - 1].Height = new GridLength(1);


          }




          Canvas c = new Canvas();
          c.Children.Add(g);

         w.Content = c;


  //        da = new DoubleAnimation( 1000 , new Duration(TimeSpan.FromSeconds(3000)));
  //       da.AccelerationRatio = 0.2;
   //      da.DecelerationRatio = 0.8;

    //     PointAnimation pa = new PointAnimation(new System.Windows.Point(0, 2000), new Duration( TimeSpan.FromSeconds(3000)));

         


          tt = new TranslateTransform();
         c.RenderTransform = tt;

    //     DoubleAnimation da = new DoubleAnimation( 100 , new Duration(TimeSpan.FromSeconds(30)));


   //     VectorAnimation va = new VectorAnimation( new Vector(0, 30), new Duration( TimeSpan.FromSeconds(100)));
 //        tt.BeginAnimation( TranslateTransform.YProperty, da );

  //       c.BeginAnimation(Canvas.RenderTransformProperty., da);





         w.MouseWheel += new System.Windows.Input.MouseWheelEventHandler(w_MouseWheel);


         //     CompositionTarget.Rendering += new EventHandler(CompositionTarget_Rendering);
     //    w.MouseDown += new System.Windows.Input.MouseButtonEventHandler(w_MouseDown);

         w.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(w_MouseLeftButtonDown);


         w.Show();
         app.Run();




      }

      static void w_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
      {
         tt.Y += 0.01;
      }

      //static void w_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
      //{
      //   tt.Y += 0.01;
      //}

      static void w_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
      {
         System.Windows.Media.Matrix m = g.RenderTransform.Value;

         if (e.Delta > 0)
         {
            m.ScaleAt(1.5, 1.5, e.GetPosition(w).X, e.GetPosition(w).Y);
         }
         else
         {
            m.ScaleAt(1.0 / 1.5, 1.0 / 1.5, e.GetPosition(w).X, e.GetPosition(w).Y);
         }

         g.RenderTransform = new MatrixTransform(m);
      }


 


   }




   class text_canvas : Canvas
   {
      public text_canvas(float size, string text, int width, int height, string font_family)
      {
         Bitmap b = new Bitmap(width, height);
         Graphics bitmapGraphics = Graphics.FromImage(b);


  //       bitmapGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  //       bitmapGraphics.InterpolationMode = InterpolationMode.
         //bitmapGraphics.CompositingMode = CompositingMode.SourceOver;
         //bitmapGraphics.PixelOffsetMode = PixelOffsetMode.Half;//
     //    bitmapGraphics.SmoothingMode = SmoothingMode.HighSpeed;
         //bitmapGraphics.TextRenderingHint = System.Drawing.Text;

   //      Bitmap b = new Bitmap(1, 2, 4, System.Drawing.Imaging.PixelFormat.Format32bppRgb, 


         bitmapGraphics.Clear(System.Drawing.Color.White);
         bitmapGraphics.DrawString(text, new Font(font_family, size), System.Drawing.Brushes.Black, new PointF(0, 0));

         System.Windows.Media.Imaging.BitmapSource bitmapSource = 
            System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
               b.GetHbitmap(),
               IntPtr.Zero,
               Int32Rect.Empty,
               System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

         System.Windows.Controls.Image i = new System.Windows.Controls.Image();
         i.Source = bitmapSource;

         this.Children.Add(i);
         this.Width = width;
         this.Height = height;

         this.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
         this.VerticalAlignment = VerticalAlignment.Top;
         this.SnapsToDevicePixels = true;
         RenderOptions.SetEdgeMode(this, EdgeMode.Aliased);
  //       RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.NearestNeighbor);

         bitmapGraphics.Dispose();
         b.Dispose();

      }
   }
}
